//
//  UIButton+QuickDev.m
//  QuickDev
//
//  Created by 胡超 on 2018/1/29.
//  Copyright © 2018年 胡超., ltd. All rights reserved.
//

#import <objc/runtime.h>

#import "UIButton+Mango.h"
#import "UIView+Mango.h"
#import "Utils.h"

static void mg_UIButton_setVImageTitleLayout(UIButton *self, CGSize imageOffset, CGSize titleOffset) {
    [self.titleLabel sizeToFit];
    
    CGFloat offset = (self.height - self.imageView.height - self.titleLabel.height) / 2;
    // imageView
    self.imageView.centerX = self.width/2 + imageOffset.width;
    self.imageView.top = offset + imageOffset.height;
    // titleLabel
    [self.titleLabel setCenterX:self.width/2 + titleOffset.width];
    [self.titleLabel setBottom:self.height - offset + titleOffset.height];
}

static void mg_UIButton_setVTitleImageLayout(UIButton *self, CGSize imageOffset, CGSize titleOffset) {
    [self.titleLabel sizeToFit];
    
    CGFloat offset = (self.height - self.imageView.height - self.titleLabel.height) / 2;
    // titleLabel
    [self.titleLabel setCenterX:self.width/2 + titleOffset.width];
    [self.titleLabel setTop:offset + titleOffset.height];
    // imageView
    self.imageView.centerX = self.width/2 + imageOffset.width;
    self.imageView.top = self.height - offset + imageOffset.height;
}

static void mg_UIButton_setHImageRightLayout(UIButton *self, CGSize imageOffset, CGSize titleOffset) {
    [self.titleLabel sizeToFit];
    
    CGFloat offset = (self.width - self.imageView.width - self.titleLabel.width) / 2;
    // titleLabel
    [self.titleLabel setLeft:offset + titleOffset.width];
    [self.titleLabel setCenterY:self.height/2 + titleOffset.height];
    // imageView
    self.imageView.right = self.width - offset + imageOffset.width;
    self.imageView.centerY = self.height/2 + imageOffset.height;
}

static void(*mg_orig_UIButton_layoutSubviews)(UIButton *, SEL);
static void mg_UIButton_layoutSubviews(UIButton *self, SEL _cmd) {
    mg_orig_UIButton_layoutSubviews(self, _cmd);
    
    NSValue *imageSizeValue = objc_getAssociatedObject(self, "mg_ImageSize");
    if (imageSizeValue) {
        CGSize imageSize = imageSizeValue.CGSizeValue;
        self.imageView.size = imageSize;
    }
    
    NSNumber *mgLayoutType = objc_getAssociatedObject(self, "mg_LayoutType");
    if (mgLayoutType == nil || mgLayoutType.intValue == MGButtonLayoutTypeOrigin)
        return;
    
    if (self.imageView.isHidden || self.titleLabel.isHidden)
        return;
    
    CGSize imageOffset = [objc_getAssociatedObject(self, "mg_ImageOffset") CGSizeValue];
    CGSize titleOffset = [objc_getAssociatedObject(self, "mg_TitleOffset") CGSizeValue];
    switch (mgLayoutType.integerValue) {
        case MGButtonLayoutTypeVImageTitle: mg_UIButton_setVImageTitleLayout(self, imageOffset, titleOffset); break;
        case MGButtonLayoutTypeVTitleImage: mg_UIButton_setVTitleImageLayout(self, imageOffset, titleOffset); break;
        case MGButtonLayoutTypeHImageRight: mg_UIButton_setHImageRightLayout(self, imageOffset, titleOffset); break;
        default:
            break;
    }
}

@interface MGUIButtonStyle: NSObject
@property (nonatomic,assign) CGFloat cornerRadius;
@property (nonatomic,assign) UIEdgeInsets insets;
@end

@implementation MGUIButtonStyle
@end

@implementation UIButton (Mango)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        MGHookMessageEx(self.class, @selector(layoutSubviews), (IMP)mg_UIButton_layoutSubviews, (IMP*)&mg_orig_UIButton_layoutSubviews);
    });
}

- (void)setImageSize:(CGSize)imageSize {
    objc_setAssociatedObject(self, "mg_ImageSize", [NSValue valueWithCGSize:imageSize], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setLayoutType:(MGButtonLayoutType)type {
    [self setLayoutType:type imageOffset:CGSizeZero titleOffset:CGSizeZero];
}

- (void)setLayoutType:(MGButtonLayoutType)type imageOffset:(CGSize)imageOffset {
    [self setLayoutType:type imageOffset:imageOffset titleOffset:CGSizeZero];
}

- (void)setLayoutType:(MGButtonLayoutType)type titleOffset:(CGSize)titleOffset {
    [self setLayoutType:type imageOffset:CGSizeZero titleOffset:titleOffset];
}

- (void)setLayoutType:(MGButtonLayoutType)type imageOffset:(CGSize)imageOffset titleOffset:(CGSize)titleOffset {
    objc_setAssociatedObject(self, "mg_LayoutType", @(type), OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, "mg_ImageOffset", [NSValue valueWithCGSize:imageOffset], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, "mg_TitleOffset", [NSValue valueWithCGSize:titleOffset], OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self setNeedsLayout];
}

- (MGUIButtonStyle *)mgStyle {
    MGUIButtonStyle *style = objc_getAssociatedObject(self, _cmd);
    if (style == nil) {
        style = [[MGUIButtonStyle alloc] init];
        style.cornerRadius = 0;
        style.insets = UIEdgeInsetsZero;
        objc_setAssociatedObject(self, _cmd, style, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return style;
}

- (void)setCornerRadius:(CGFloat)cornerRadius insets:(UIEdgeInsets)insets {
    self.mgStyle.cornerRadius = cornerRadius;
    self.mgStyle.insets = insets;
}

- (void)setBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth forState:(UIControlState)forState {
    [self setBackgroundImageEx:nil borderColor:borderColor borderWidth:borderWidth forState:forState];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)forState {
    [self setBackgroundImageEx:backgroundColor borderColor:nil borderWidth:0 forState:forState];
}

- (void)setBackgroundImageEx:(UIColor *)backgroundColor borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth forState:(UIControlState)forState {
    MGUIButtonStyle *style = self.mgStyle;
    UIEdgeInsets i = style.insets;
    CGRect frame = CGRectMake(i.left, i.top, self.width-i.left-i.right, self.height-i.top-i.bottom);
    UIView *view = [[UIView alloc] initWithFrame:self.bounds];
    UIView *imageView = [[UIView alloc] initWithFrame:frame];
    imageView.layer.borderColor = borderColor.CGColor;
    imageView.layer.borderWidth = borderWidth;
    imageView.layer.cornerRadius = style.cornerRadius;
    imageView.layer.masksToBounds = YES;
    imageView.backgroundColor = backgroundColor;
    [view addSubview:imageView];
    [self setBackgroundImage:view.toImage forState:forState];
}

@end
